package cn.elwy.rcp.ui.explorer.core.handler;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tmatesoft.svn.core.wc.SVNRevision;

import cn.elwy.common.util.PathUtil;
import cn.elwy.common.util.sort.FileComparator;
import cn.elwy.rcp.core.model.ui.progress.ProgressMonitor;
import cn.elwy.rcp.ui.explorer.core.PathConst;
import cn.elwy.rcp.ui.explorer.core.util.SVNClient;
import cn.elwy.rcp.ui.explorer.core.util.SvnUtil;
import cn.elwy.rcp.ui.explorer.core.vo.BuilderModule;
import cn.elwy.rcp.ui.explorer.core.vo.ProjectConfig;

public class BuilderModuleChoiceHandler extends ProjectServiceHandler {

  private Logger logger = LoggerFactory.getLogger(BuilderModuleChoiceHandler.class);
  private Comparator<File> fileComparator = FileComparator.getComparator(true);

  private SVNClient svnClient;
  private Map<String, String> outMap;
  private ProjectConfig pc;

  @Override
  public Object handler(ProjectConfig pc, ProgressMonitor monitor) {
    this.pc = pc;
    String filePath = PathUtil.getFilePath(pc.getResourcePath(), pc.getSourceDirectory());
    File file = new File(filePath);
    List<BuilderModule> builderModuleList = new ArrayList<BuilderModule>();
    newBuildeModule(file, builderModuleList, outMap);
    return builderModuleList;
  }

  private boolean newBuildeModule(File file, List<BuilderModule> builderModuleList, Map<String, String> outMap) {
    if (isIgnore(file)) {
      logger.info("加载工程时忽略文件：" + file.getAbsolutePath());
      return true;
    }
    if (file.isDirectory()) {
      File[] listFiles = file.listFiles();
      Arrays.sort(listFiles, fileComparator);
      for (File childfile : listFiles) {
        boolean result = newBuildeModule(childfile, builderModuleList, outMap);
        if (!result) {
          break;
        }
      }
    } else if (file.getName().equals("pom.xml")) {
      String resourcePath = pc.getResourcePath().replace("\\", "/");

      BuilderModule builderModule = new BuilderModule();
      String modulePath = file.getParentFile().getAbsolutePath().replace("\\", "/");
      modulePath = modulePath.replace(resourcePath, "");
      modulePath = PathUtil.getRelativePath(modulePath);
      long latestRevision = 0;
      try {
        latestRevision = SvnUtil.getRevision(svnClient.getRepository(), modulePath);
      } catch (Exception e) {
        logger.warn("获取版本号错误：", e);
      }

      SVNRevision svnRevision = SvnUtil.getSVNRevision(pc.getStartVersion());
      long startVersion = svnRevision.getNumber();
      builderModule.setBuilder(startVersion < latestRevision);
      builderModule.setHasUpdate(startVersion < latestRevision);

      String absolutePath = file.getAbsolutePath();
      builderModule.setFilePath(absolutePath);

      absolutePath = absolutePath.replace("\\", "/");
      String filePath = PathUtil.getFilePath(resourcePath, pc.getSourceDirectory());
      absolutePath = absolutePath.replace(filePath, "");
      builderModule.setModulePath(modulePath);

      String outDirectory = outMap.get(file.getParentFile().getName());
      if (outDirectory == null) {
        outDirectory = PathConst.DEFAULT_OUT_DIR;
      }
      builderModule.setOutDirectory(outDirectory);
      builderModuleList.add(builderModule);
      return false;
    }
    return true;
  }

  private boolean isIgnore(File file) {
    String[] ignoreDir = PathConst.IGNORE_BUILDER_DIR;
    for (String dir : ignoreDir) {
      if (file.getName().equalsIgnoreCase(dir)) {
        return true;
      }
    }
    return false;
  }

  public SVNClient getSvnClient() {
    return svnClient;
  }

  public void setSvnClient(SVNClient svnClient) {
    this.svnClient = svnClient;
  }

  public Map<String, String> getOutMap() {
    return outMap;
  }

  public void setOutMap(Map<String, String> outMap) {
    this.outMap = outMap;
  }

}
